﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "link.h"
#include "entity.h"
#include <QClipboard>

/* 模拟一个简单的实体和链接的创建，复制(只能单个实体)和粘贴*/
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Graphics");

    m_pScene = new QGraphicsScene(0, 0, 600, 500);

    m_pView = new QGraphicsView;
    m_pView->setScene(m_pScene);
    m_pView->setDragMode(QGraphicsView::RubberBandDrag);
    m_pView->setRenderHints(QPainter::Antialiasing
                         | QPainter::TextAntialiasing);
    m_pView->setContextMenuPolicy(Qt::ActionsContextMenu);
    this->setCentralWidget(m_pView);

    seqNumber = 0;

    initActions();
    initToolBars();

    connect(m_pScene, SIGNAL(selectionChanged()),
            this, SLOT(updateActions()));

    updateActions();
    resize(900, 600);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::addEntity()
{
    Entity *pEntity = new Entity;
    pEntity->setText(tr("Entity %1").arg(seqNumber + 1));
    setupEntity(pEntity);
}

void MainWindow::addLink()
{
    EntityPair entitys = selectedEntityPair();
    if (entitys == EntityPair())
        return;

    Link *link = new Link(entitys.first, entitys.second);
    m_pScene->addItem(link);
}

void MainWindow::del()
{
    QList<QGraphicsItem *> items = m_pScene->selectedItems();
    QMutableListIterator<QGraphicsItem *> i(items);
    while (i.hasNext()) {
        Link *link = dynamic_cast<Link *>(i.next());
        if (link) {
            delete link;
            i.remove();
        }
    }

    qDeleteAll(items);
}

void MainWindow::copy()
{
    Entity *pEntity = selectedEntity();
    if (!pEntity)
        return;

    QString str = QString("Entity %1 %2 %3 %4")
                  .arg(pEntity->textColor().name())
                  .arg(pEntity->outlineColor().name())
                  .arg(pEntity->backGroundColor().name())
                  .arg(pEntity->text());
    QApplication::clipboard()->setText(str);
}

void MainWindow::paste()
{
    QString str = QApplication::clipboard()->text();
    QStringList parts = str.split(" ");

    //每行五个图
    if (parts.count() >= 5 && parts.first() == "Entity") {
        Entity *pEntity = new Entity;
        pEntity->setText(QStringList(parts.mid(4)).join(" "));
        pEntity->setTextColor(QColor(parts[1]));
        pEntity->setOutlineColor(QColor(parts[2]));
        pEntity->setBackgroundColor(QColor(parts[3]));
        setupEntity(pEntity);
    }
}

void MainWindow::updateActions()
{
    bool hasSelection = !m_pScene->selectedItems().isEmpty();
    bool isEntity = (selectedEntity() != 0);
    bool isEntityPair = (selectedEntityPair() != EntityPair());

    ui->copyAct->setEnabled(isEntity);
    ui->addLinkAct->setEnabled(isEntityPair);
    ui->deleteAct->setEnabled(hasSelection);

    foreach (QAction *action, m_pView->actions())
        m_pView->removeAction(action);

    foreach (QAction *action, ui->menu->actions()) {
        if (action->isEnabled())
            m_pView->addAction(action);
    }
}

void MainWindow::initActions()
{
    ui->addEntityAct->setIcon(QIcon(":/images/node.png"));
    connect(ui->addEntityAct, SIGNAL(triggered()), this, SLOT(addEntity()));

    ui->addLinkAct->setIcon(QIcon(":/images/link.png"));
    connect(ui->addLinkAct, SIGNAL(triggered()), this, SLOT(addLink()));

    ui->deleteAct->setIcon(QIcon(":/images/delete.png"));
    ui->deleteAct->setShortcut(tr("Del"));
    connect(ui->deleteAct, SIGNAL(triggered()), this, SLOT(del()));

    ui->copyAct->setIcon(QIcon(":/images/copy.png"));
    ui->copyAct->setShortcut(tr("Ctrl+C"));
    connect(ui->copyAct, SIGNAL(triggered()), this, SLOT(copy()));

    ui->pasteAct->setIcon(QIcon(":/images/paste.png"));
    ui->pasteAct->setShortcut(tr("Ctrl+V"));
    connect(ui->pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
}

void MainWindow::initToolBars()
{
    ui->mainToolBar->addAction(ui->addEntityAct);
    ui->mainToolBar->addAction(ui->addLinkAct);
    ui->mainToolBar->addAction(ui->deleteAct);
    ui->mainToolBar->addAction(ui->copyAct);
    ui->mainToolBar->addAction(ui->pasteAct);
}

void MainWindow::setZValue(int z)
{
    Entity *pEntity = selectedEntity();
    if (pEntity)
        pEntity->setZValue(z);
}

void MainWindow::setupEntity(Entity *pEntity)
{
    pEntity->setPos(QPoint(80 + (100 * (seqNumber % 5)),
                        80 + (50 * ((seqNumber / 5) % 7))));
    m_pScene->addItem(pEntity);
    ++seqNumber;

    m_pScene->clearSelection();
    pEntity->setSelected(true);
}

Entity *MainWindow::selectedEntity() const
{
    QList<QGraphicsItem *> items = m_pScene->selectedItems();
    if (items.count() == 1) {
        return dynamic_cast<Entity *>(items.first());
    } else {
        return 0;
    }
}

Link *MainWindow::selectedLink() const
{
    QList<QGraphicsItem *> items = m_pScene->selectedItems();
    if (items.count() == 1) {
        return dynamic_cast<Link *>(items.first());
    } else {
        return 0;
    }
}

MainWindow::EntityPair MainWindow::selectedEntityPair() const
{
    QList<QGraphicsItem *> items = m_pScene->selectedItems();
    if (items.count() == 2) {
        Entity *pFirst = dynamic_cast<Entity *>(items.first());
        Entity *pSecond = dynamic_cast<Entity *>(items.last());
        if (pFirst && pSecond)
            return EntityPair(pFirst, pSecond);
    }
    return EntityPair();
}
